package com.tedu.egmvcboot.interceptor;

import com.tedu.egmvcboot.pojo.vo.UserVO;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /*
            HttpServletRequest请求对象中提供了一个方法:
            getSession()，该方法可以获取到对应当前客户端的会话对象
         */
        HttpSession session = request.getSession();
        //通过Session获取用户的登录信息
        UserVO user = (UserVO) session.getAttribute("userVO");
        if(user == null){//没有登录
            //设置响应对象，要求浏览器请求"/index.html"页面内容
            response.sendRedirect("/index.html");
            return false;//拦截该请求
        }else{
            return true;//放行
        }
    }
}
